jQuery(document).ready(function()
{
    var page_number = 1;
    var offset = 0;
    const limit = 12;
    
    show_images(offset, limit);
    fadeInImages();
    jQuery(".prev-slide").click(function()
    {
        page_number--;
        if(page_number < 1)
        {
            page_number = 1;
            offset = 0;
        }
        else
        {
            offset = ((page_number * 12) - 12);
            show_images(offset, limit);
            fadeInImages();
        }
        
    });

    jQuery(".next-slide").click(function()
    {
    	page_number++;
        offset = ((page_number * 12) - 12);
        
        jQuery.ajax(
        {
            type: "post",
            data: {'offset' : offset, 'limit' : limit},
            url: "index.php/ajaxquery/getimages",
            dataType: 'html',
            success: function(data)
            {
                if(data)
                {
                    jQuery(".slide-wrap").html(data);
                    jQuery('.slide-wrap a').lightBox();
                }
                else
                {
                    page_number--;
                }
            }
        });
        
        fadeInImages();
    });
});

function show_images(offset, limit)
{
    jQuery.ajax(
    {
	    type: "post",
	    data: {'offset' : offset, 'limit' : limit},
	    url: "index.php/ajaxquery/getimages",
	    dataType: 'html',
  	    success: function(data)
  		{
  			if(data)
    	    {
    		    jQuery(".slide-wrap").html(data);
    		    jQuery('.slide-wrap a').lightBox();
    		    
    		}
    		else
    		{
    		    page_number--;
            }

        }
    });

}

function fadeInImages()
{
    function randomNum()
    {
	    var min = 1;
	    var max = 12;
	    var random = min - 0.5 + Math.random() * (max - min + 1);
	    return Math.round(random);
    }
	
    var img_array = [];
    
    while(img_array.length < 12)
    {
	    var i = randomNum();		
	    if(jQuery.inArray(i, img_array) > -1)
	    {
		    continue;
        }
        else
        {
		    img_array.push(i);
        }
    }

    var i = 0;
    var interval = setInterval(function()
	    {
		    jQuery('.slide-wrap a:nth-child('+img_array[i]+')').
		        animate({opacity: 1}, 400);
		    i++;
            if(i == 12)
            {
			    clearInterval(interval);
			}
        }, 150);
}




